/**

 @Name：cctvcc 用户管理

 */

layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , setter = layui.setter
        , view = layui.view
        , form = layui.form;

    var cols = [[
        {type: 'checkbox', fixed: 'left'}
        , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
        // , {field: 'id', width: 80, title: 'ID', sort: true}
        , {field: 'userName', title: '登录名'}
        , {field: 'nickName', title: '用户昵称'}
        , {field: 'phonenumber', title: '用户昵称'}
        , {field: 'sex', title: '性别'}
        , {field: 'avatar', title: '头像', width: 100, templet: '#imgTpl'}
        , {field: 'email', title: '邮箱'}
        , {field: 'role', title: '角色'}
        , {
            field: 'createTime'
            , title: '加入时间'
            , sort: true
            ,templet:function(val){
                return view.crtTimeFtt(setter.dataFormat.yyyymmddhhmm, val.createTime, true);
            }
        }
        , {field: 'status', title: '状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
    ]];

    // 列表
    table.render(view.tableInit({
        elem: '#LAY-user-back-manage'
        ,url: layui.setter.ctx + '/system/sysUser/list'
        ,cols: cols
    }));

    //监听搜索
    form.on('submit(LAY-user-back-search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-user-back-manage', {
            where: field
        });
    });

    //事件
    var active = {
        batchdel: function () {
            var checkStatus = table.checkStatus('LAY-user-back-manage')
                , checkData = checkStatus.data; //得到选中的数据

            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }

            layer.prompt({
                formType: 1
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                layer.close(index);

                layer.confirm('确定删除吗？', function (index) {

                    //执行 Ajax 后重载
                    /*
                    admin.req({
                      url: 'xxx'
                      //,……
                    });
                    */
                    table.reload('LAY-user-back-manage');
                    layer.msg('已删除');
                });
            });
        }
        , add: function () {
            layer.open({
                type: 2
                , title: '添加管理员'
                , content: 'adminform.html'
                , area: ['420px', '420px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-back-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});
                        table.reload('LAY-user-front-submit'); //数据刷新
                        layer.close(index); //关闭弹层
                    });

                    submit.trigger('click');
                }
            });
        }
    };

    $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    table.on('tool(LAY-user-back-manage)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.prompt({
                formType: 1
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                layer.close(index);
                layer.confirm('确定删除此管理员？', function (index) {
                    console.log(obj)
                    obj.del();
                    layer.close(index);
                });
            });
        } else if (obj.event === 'edit') {
            var tr = $(obj.tr);

            layer.open({
                type: 2
                , title: '编辑管理员'
                , content: '../../../views/user/administrators/adminform.html'
                , area: ['420px', '420px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-back-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});
                        table.reload('LAY-user-front-submit'); //数据刷新
                        layer.close(index); //关闭弹层
                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            })
        }
    });


    exports('sysUser', {})
});